package com.leetcode.stack_queue;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author Dennis Li
 * @date 2020/7/21 16:35
 */
public class StackImplementQueue_232 {

    Deque<Integer> stack1;
    Deque<Integer> stack2;

    /**
     * Initialize your data structure here.
     */
    public void MyQueue() {
        stack1 = new LinkedList<>();
        stack2 = new LinkedList<>();
    }

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
        stack1.push(x);
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    public synchronized int pop() {
        if(!stack2.isEmpty())
            return stack2.pop();
        while (!stack1.isEmpty())
            stack2.push(stack1.pop());
        return stack2.pop();
    }

    /**
     * Get the front element.
     */
    public synchronized int peek() {
        if(!stack2.isEmpty())
            return stack2.peek();
        while (!stack1.isEmpty())
            stack2.push(stack1.pop());
        return stack2.peek();
    }

    /**
     * Returns whether the queue is empty.
     */
    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }

}
